/*!
    \copyright  Copyright (c) 2023-2024 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \version    
    \file       google_finder_advertiser.h
    \addtogroup  google_finder
    \brief      Header file for the component handling Google Finder Advertisement.
    @{
*/

#ifndef GOOGLE_FINDER_ADVERTISER_H_
#define GOOGLE_FINDER_ADVERTISER_H_

#ifdef INCLUDE_GOOGLE_FINDER

#include "le_advertising_manager.h"
#include "google_finder_private.h"
#include <bdaddr.h>

#define GOOGLE_FINDER_FLAGS_IS_UNWANTED_TRACKING_PROTECTION_MODE_SET(raw_flags) \
           (((raw_flags) & GOOGLE_FINDER_ADV_HASHED_FLAGS_UNWANTED_TRACKING_PROT_MODE)== \
            GOOGLE_FINDER_ADV_HASHED_FLAGS_UNWANTED_TRACKING_PROT_MODE)

#define GOOGLE_FINDER_FLAGS_SET_UNWANTED_TRACKING_PROTECTION_MODE(raw_flags) \
           ((raw_flags) |= GOOGLE_FINDER_ADV_HASHED_FLAGS_UNWANTED_TRACKING_PROT_MODE)

#define GOOGLE_FINDER_FLAGS_RESET_UNWANTED_TRACKING_PROTECTION_MODE(raw_flags) \
           ((raw_flags) &= ~GOOGLE_FINDER_ADV_HASHED_FLAGS_UNWANTED_TRACKING_PROT_MODE)

#define GOOGLE_FINDER_GET_NEXT_TIME_COUNTER_FOR_EID_UPDATE(time_counter) \
            (((time_counter) & GOOGLE_FINDER_ADV_MASK_TIME_COUNTER_VALUE) + \
            GOOGLE_FINDER_ADV_PUBLIC_KEY_TIME_WINDOW)

#define GOOGLE_FINDER_IS_CURRENT_TIME_NEARING_MSB_UPDATE(time_counter) \
            ((GOOGLE_FINDER_GET_NEXT_TIME_COUNTER_FOR_EID_UPDATE(time_counter) - (time_counter)) <=\
            GOOGLE_FINDER_ADV_TIME_COUNTER_RELIEF_WINDOW)

/* First batttery state should be reset before calling this macro, as its multibit flag */
#define GOOGLE_FINDER_FLAGS_SET_BATTERY_STATE(raw_flags, state) \
           ((raw_flags) |= ((state) & GOOGLE_FINDER_ADV_HASHED_FLAGS_BATTERY_BIT_MASK))
#define GOOGLE_FINDER_FLAGS_RESET_BATTERY_STATE(raw_flags) \
           ((raw_flags) &= ~GOOGLE_FINDER_ADV_HASHED_FLAGS_BATTERY_BIT_MASK)
#define GOOGLE_FINDER_FLAGS_GET_BATTERY_STATE(raw_flags) \
           ((raw_flags) & GOOGLE_FINDER_ADV_HASHED_FLAGS_BATTERY_BIT_MASK)

/* 
 *  Normal if >= 25% 
 *  Low if < 25% and >= 10%
 *  Critical < 10%
 */
#define GOOGLE_FINDER_BATTERY_NORMAL_PERCENTAGE_MIN_THRESHOLD          25
#define GOOGLE_FINDER_BATTERY_LOW_PERCENTAGE_MIN_THRESHOLD             10

/*! \brief Google Finder Advertisement States */
typedef enum google_finder_adv_states
{
    GOOGLE_FINDER_ADV_IDLE_STATE,
    GOOGLE_FINDER_ADV_GENERATE_AES_ECB_256_STATE,
    GOOGLE_FINDER_ADV_GENERATE_PRIVATE_KEY_STATE,
    GOOGLE_FINDER_ADV_GENERATE_PUBLIC_KEY_STATE,
    GOOGLE_FINDER_ADV_GENERATED
} googleFinderAdvDataState;

typedef struct
{
    uint8                           *public_key;
    uint32                          time_counter_used_for_public_key;
}google_finder_public_key_data_t;

/*! \brief Global data structure for Google Finder Adverts */
typedef struct
{
    le_adv_item_handle              adv_register_handle;
    google_finder_public_key_data_t public_key_adv_data;
    uint8                           private_key_hash;              /* one byte hash of private key */
    uint8                           raw_flags;                     /* raw data for hashed flags */
    googleFinderAdvDataState        adv_data_state;
    bdaddr                          adv_bdaddr;
    bool                            adverts_enabled : 1;
    bool                            random_address_rotation_timeout_event_notified : 1;
    bool                            is_generated_public_key_unused : 1;
    bool                            eik_updated_pending_eid_change : 1;
}google_finder_advert_data_t;


/*! \brief Function to update hashed flags in advertisement. This also 
           invokes advertising manager to update overall finder advertisements.
    \param void
    \return void
*/
void googleFinderAdv_UpdateHashedFlagsAdvData(void);

/*! \brief Function to kick start advertising data change process for Finder.
    \param void
    \return void
*/
void googleFinderAdv_KickAdvDataChange(void);

/*! \brief Function which constructs Eddystone-E2EE-EID frame.
    \param force_construct_eddystone_frame Parameter to distinguish between normal eddystone frame generation due to time counter MSB bit change
           and force to generate the next eddystone frame due to RPA timeout.
    \return void
*/
void googleFinderAdv_ConstructEddystoneFrame(bool force_construct_eddystone_frame);

/*! \brief Function which stops advertisement of Eddystone-E2EE-EID frame.
    \param void
    \return void
*/
void googleFinderAdv_StopAdvertisement(void);

/*! \brief Function which starts advertisement of Eddystone-E2EE-EID frame.
    \param void
    \return void
*/
void googleFinderAdv_StartAdvertisement(void);

/*! \brief Private API to handle connection library crypto messages.
    \param id - Message ID
    \param message - Message Data
    \return void
*/
void googleFinderAdv_HandleCryptoMessage(MessageId id, Message message);

/*! \brief Private API to update Google Finder EID data in advertisement.
    \param void
    \return void
*/
void googleFinderAdv_HandleUpdatedEid(void);

/*! \brief Process the deferred beacon action signals upon successful calculation of EID
    \param void
    \return void
 */
void googleFinder_ProcessDeferredBeaconAction(void);

/*! \brief Private API to setup Google Finder Advertisement.
    \param void
    \return void
*/
void googleFinderAdv_SetUpAdvertising(void);
#endif /* INCLUDE_GOOGLE_FINDER */
#endif /* GOOGLE_FINDER_ADVERTISER_H_ */
/**! @} !*/